package com.briup.store.web.controller;

import com.briup.common.response.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/uploads")
public class UploadController {

    /**
     * 本地上传
     */
    @PostMapping
    public Result locationUpload(@RequestPart MultipartFile file) {
        //拿到文件原始名称
        String originalFilename = file.getOriginalFilename();
        //UUID
        String fileName = UUID.randomUUID().toString();
        //源文件后缀名  sdq.jpg   a.txt.jpg   a.x.x.x.x.jpg
        String[] split = originalFilename.split("[.]");
        String suffix = split[split.length - 1];

        //文件名称
        fileName = fileName + "." + suffix;
        try {
            file.transferTo(new File("D:\\code\\idea_code\\store\\src\\main\\resources\\image\\" +fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success("http://localhost:8888/img/"+fileName);
    }

}
